using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectDistrict()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectDistrictByIDCity(int idcity)
		{
			var data = new DataTransferViewModel
			{
				IDCity = idcity,
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT,
			};
			return View("SelectDistrict", CreateViewModel(data));
		}

		public ActionResult SelectDistrictByIDCityPaging(int idcity)
		{
			var data = new DataTransferViewModel
			{
				IDCity = idcity,
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT,
			};
			return PartialView("Templates/TH_ListDistrict", CreateViewModel(data));
		}


		public ActionResult DeleteDistrict(int id, int idcity, int page = 1)
		{
			var result = _repDistrict.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT,
				CurrentPage = page,
                IDCity = idcity
			};
			return View("SelectDistrict", CreateViewModel(data));
		}

		public ActionResult EditDistrict(int id, int idcity, int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT,
				CurrentPage = page,
				IntID = id,
                IDCity = idcity
			};
			return PartialView("Templates/TH_EditDistrict", CreateViewModel(data));
		}

		public ActionResult CancelEditingDistrict(int idcity, int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT,
				CurrentPage = page,
                IDCity = idcity
			};
			return PartialView("Templates/TH_ListDistrict", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateDistrict(FormCollection forms)
		{
			string id = forms.GetValues("District_ID")[0];
			string name = forms.GetValues("District_Name")[0];
			var district = _repDistrict.SelectByID(int.Parse(id));
			district.Name = name;
			var result = _repDistrict.Update(district);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT,
                IDCity = district.IDCity
			};
			return View("SelectDistrict", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertDistrict(FormCollection forms)
		{
			string idcity = forms.GetValues("District_IDCity")[0];
			string name = forms.GetValues("District_Name")[0];
			var newItem = new DistrictDTO
			{
				IDCity = int.Parse(idcity),
				Name = name
			};
			var result = _repDistrict.Insert(newItem);
			var data = new DataTransferViewModel
			{
				IDCity = int.Parse(idcity),
				EnumViewModelType = EnumViewModel.ADMIN_DISTRICT,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectDistrict", CreateViewModel(data));
		}


	}
}